package ui;

import network.server.IServer;
import network.server.SocketServer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: grigory.rozhkov
 * Date: 13.09.12
 * Time: 0:43
 * To change this template use File | Settings | File Templates.
 */
public class MainPanel extends JPanel {
    private JButton up;
    private JButton down;
    private IServer server;

    public MainPanel (int width, int height) {
        this.setBackground(Color.WHITE);
        this.setLayout(null);
        this.setSize(width, height);

        server = new SocketServer();

        up = new JButton("UP");
        up.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                changeButtonsState();
                upServer();
            }
        });
        up.setBounds(0, 0, width / 2, height);
        this.add(up);
        up.setEnabled(true);

        down = new JButton("DOWN");
        down.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                changeButtonsState();
                downServer();
            }
        });
        down.setBounds(width / 2, 0, width / 2, height);
        this.add(down);
        down.setEnabled(false);
    }

    private void changeButtonsState(){
        down.setEnabled(!down.isEnabled());
        up.setEnabled(!up.isEnabled());
    }

    private void downServer() {
        server.down();
    }

    private void upServer() {
        //avoiding closed socket exception if user clicks on up&down butons very fast
        server.up();
    }
}
